<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Sowing extends Backend
{
    
    /**
     * Sowing模型对象
     * @var \app\admin\model\Sowing
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Sowing;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    //主页
    public function sowingmap(){
        $result = model('sowing') -> where(['uid' => session('userinfo:id')]) -> paginate(3);
        $page = $result -> render();

        $this -> assign('page', $page);
        $this -> assign('sowing', $result);
        return view();
    }
    //添加轮播图
    public function addsowing(){
        $input = input('post.');
        $uid = session('userinfo:id');
        $sowarr = [];
        if(!empty($input['sow_img_pc'])) {
            $sowarr[] = [
                'uid' => $uid,
                'link' => $input['link'],
                'status' => '1',
                'sow_img' => $input['sow_img_pc']
            ];
        }
        if(!empty($input['sow_img_mobile'])) {
            $sowarr[] = [
                'uid' => $uid,
                'link' => $input['link'],
                'status' => '2',
                'sow_img' => $input['sow_img_mobile']
            ];
        }
        foreach ($sowarr as $value) {
            $result = $insertResult = model('sowing') -> insert($value);
            if(!$result) {
                return json(['status' => 0, 'info' => '第'.$value['status'].'张图片插入失败!']);
            }
        }
        return json(['status' => 1, 'info' => '添加成功!']);
    }

    //获取一个轮播
    public function getOneSowing(){
        $id = input('post.id');
        $selectOne = model('sowing') -> where(['id' => $id]) -> find();
        return json(['status' => 1, 'data' => $selectOne]);
    }
    //修改一个轮播图
    public function editSubmitSowing(){
        $input = input('post.');
        if(!empty($input['sow_img1'])){
            $input['sow_img'] =  $input['sow_img1'];
            unset($input['sow_img1']);
        }
        if(!empty($input['sow_img2'])){
            $input['sow_img'] =  $input['sow_img2'];
            unset($input['sow_img2']);
        }

        $updateResult = model('sowing') -> where(['id' => $input['id']]) -> update($input);
        if($updateResult) {
            return json(['status' => 1, 'info' => '更新成功!']);
        }
        return json(['status' => 0, 'info' => '更新失败!']);
    }
    //删除一个轮播图
    public function delOneSowing() {
        $id = input('id');
        $delResult = model('sowing') -> where(['id' => $id, 'uid' => session('userinfo:id')]) -> delete();
        if($delResult) {
            echo "<script>alert('删除成功!');top.location.href='/admin/sowing/sowingmap?ref=addtabs'</script>";
            die;
        }else{
            echo "<script>alert('删除失败!');top.location.href='/admin/sowing/sowingmap?ref=addtabs'</script>";
            die;
        }

    }
    //七牛上传
    public function qiniu() {
        $file = $this -> request -> file('myfile');
        $filePath = $file -> getInfo('tmp_name');

        $ext = pathinfo($file -> getInfo('name'), PATHINFO_EXTENSION);
        $key = 'upload/'.date('Ymdhis').substr(md5($file -> getRealPath()), 0, 5). rand(0, 999).'.'.$ext;

        vendor('qiniu.autoload');
        $accessKey = config('ACCESSKEY');
        $secretKey = config('SECRETKEY');
        $auth = new \Qiniu\Auth($accessKey, $secretKey);
        $bucket = config('BUCKET');
        $domain = config('domain');
        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        $uploaderMgr = new \Qiniu\Storage\UploadManager();
        //调用putFile方法上传文件
        list($ret, $err) = $uploaderMgr -> putFile($token, $key, $filePath);
        if($err !== null) {
            return json(['status' => 0, 'info' => $err]);
        }else {
            return json(['status' => 1, 'info' => '上传成功！', 'data' => '//'.$domain.'/'.$ret['key']]);
        }
    }
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

}
